#!/usr/bin/env python
# -*- coding: utf-8 -*-
# by vellhe 2017/7/9
import logging
import os
import time

from flask import Flask, request, render_template, Blueprint, Response

# Flask初始化参数尽量使用你的包名，这个初始化方式是官方推荐的，官方解释：http://flask.pocoo.org/docs/0.12/api/#flask.Flask

app = Flask(__name__)
bp = Blueprint('videoEdit', __name__, template_folder="templates", static_folder="static", static_url_path="/static")
app.config['JSON_AS_ASCII'] = False
if app.json and app.json.ensure_ascii:
    app.json.ensure_ascii = False  # 解决中文乱码问题
# 文件上传目录
app.config['UPLOAD_FOLDER'] = 'static/uploads/'
if not os.path.exists('static/uploads/'):
    os.makedirs('static/uploads/')

log = logging.getLogger("app")
app.config['JSON_AS_ASCII'] = False
app.jinja_env.variable_start_string = '${'  # 解决与vue 标签的冲突
app.jinja_env.variable_end_string = '}'


# app.json.ensure_ascii = False  # 解决中文乱码问题


@bp.route('/')
def index():
    return render_template('index.html')


@bp.route('/stream', methods=['GET'])
def stream():
    print(request.path)
    print(request.full_path)
    print(request.url)

    def events():
        for i in range(50):
            yield "data: %s %d\n\n" % (i, i)
            time.sleep(.1)  # an artificial delay

    return Response(events(), content_type='text/event-stream')


@bp.before_request
def before():
    log.debug("before_request")
    pass


@bp.after_request
def after(response):
    log.debug("after_request")
    return response


app.register_blueprint(bp, url_prefix='/')

if __name__ == "__main__":
    # 这种是不太推荐的启动方式，我这只是做演示用，官方启动方式参见：http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
    app.run(host="0.0.0.0", port=8888, debug=True)
